package com.sne.sys.controller;


import com.sne.common.entity.Result;
import com.sne.common.exception.CustomException;
import com.sne.sys.entity.SysMenu;
import com.sne.sys.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单 前端控制器
 * </p>
 *
 * @author fanghh
 * @since 2020-01-03
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/sys/sys-menu")
@Api(tags = "菜单管理")
public class SysMenuController {

    @Autowired
    private ISysMenuService iSysMenuService;

    @ApiOperation(value = "菜单树")
    @GetMapping("/tree")
    public Result findTree() {
        return Result.success(iSysMenuService.findTree());
    }

    @PreAuthorize("hasAuthority('sys:menu:save')")
    @ApiOperation(value = "保存菜单")
    @PostMapping("/save")
    public Result save(@Valid @RequestBody SysMenu sysMenu) {
        return Result.success(iSysMenuService.save(sysMenu));
    }

    @PreAuthorize("hasAuthority('sys:menu:update')")
    @ApiOperation(value = "更新菜单")
    @PutMapping("/update")
    public Result update(@Valid @RequestBody SysMenu sysMenu) {
        return Result.success(iSysMenuService.updateById(sysMenu));
    }

    @PreAuthorize("hasAuthority('sys:menu:delete')")
    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return Result.success(iSysMenuService.deleteMenu(id));
    }

    @PreAuthorize("hasAuthority('sys:menu:deleteIds')")
    @ApiOperation(value = "批量删除字典组")
    @DeleteMapping("/deleteIds")
    public Result deleteIds(@RequestBody @Size(min = 1, message = "ids不能为空") List<Long> ids) {
        return Result.success(iSysMenuService.deleteIds(ids));
    }

    @ApiOperation(value = "根据id获取菜单")
    @GetMapping("/find/{id}")
    public Result find(@PathVariable("id") Long id) {
        return Result.success(iSysMenuService.getById(id));
    }

    @ApiOperation(value = "根据角色集合获取菜单路由")
    @PostMapping("/findRoutes")
    public Result findRoutes(@Size(min = 1,message = "角色不能为空") @RequestBody List<Long> roleIds) {
        return Result.success(iSysMenuService.findRoutes(roleIds));
    }

    @ApiOperation(value = "根据角色集合获取按钮权限")
    @PostMapping("/findPermissions")
    public Result findPermissions(@Size(min = 1,message = "角色不能为空") @RequestBody List<Long> roleIds) {
        return Result.success(iSysMenuService.findPermissions(roleIds));
    }

    @PreAuthorize("hasAuthority('sys:menu:list')")
    @ApiOperation(value = "菜单列表")
    @PostMapping("/list")
    public Result list(@RequestBody SysMenu menu){
        return Result.success(iSysMenuService.findListTree(menu));
    }

}

